//
//  DateSelectVC.swift
//  RPay
//
//  Created by CharlieLiao on 2017/8/1.
//  Copyright © 2017年 Charlie. All rights reserved.
//

class DateSelectVC: BaseVC
{
    init(startDate: Date?, endDate: Date?)
    {
        if startDate != nil {
            self.startDate = startDate!
        } else {
            self.startDate = Date(timeIntervalSince1970: 1496246400)
        }
        if endDate != nil {
            self.endDate = endDate!
        } else {
            self.endDate = Date()
        }
        super.init(nibName: "DateSelectVC", bundle: nil)
        
        modalPresentationStyle = .overCurrentContext
        modalTransitionStyle = .crossDissolve
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        // Do any additional setup after loading the view.
        
        startDatePicker.date = startDate
        startDatePicker.maximumDate = Date()
        
        endDatePicker.minimumDate = startDatePicker.date
        endDatePicker.date = endDate
        endDatePicker.maximumDate = Date()
        titleItem.title = "\((startDate as NSDate).toDateString!) 至 \((endDate as NSDate).toDateString!)"
    }
    
    
    // MARK: - Event respond
    
    @IBAction private func 点击搜索()
    {
        dismiss(animated: true, completion: nil)
        if tapSearch != nil {
            tapSearch!(startDatePicker.date, endDatePicker.date)
        }
    }
    
    @IBAction private func 点击取消()
    {
        dismiss(animated: true, completion: nil)
    }
    
    @IBAction private func dateValueChange()
    {
        endDatePicker.minimumDate = startDatePicker.date
        titleItem.title = "\((startDatePicker.date as NSDate).toDateString!) 至 \((endDatePicker.date as NSDate).toDateString!)"
    }
    
    // MARK: - Get Set
    private var startDate: Date
    private var endDate: Date
    
    var tapSearch: ((_ startDate: Date, _ endDate: Date)->Void)?
    
    @IBOutlet private var startDatePicker: UIDatePicker!
    @IBOutlet private var endDatePicker: UIDatePicker!
    @IBOutlet private var titleItem: UIBarButtonItem!
}
